//
//  ViewController.m
//  SendPostRequest
//
//  Created by 移云 on 2019/10/29.
//  Copyright © 2019 admin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self post];
}


//发送post请求
-(void)post
{
    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/php/login.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //发送post请求
    request.HTTPMethod = @"post";
    //设置请求体
    NSString *body = @"username=123&password=123";
    //把字符串转换成NSData对象
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        if (connectionError != nil)
        {
            NSLog(@"网络连接错误");
            return;
        }
        
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
        if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304)
        {
            //解析数据
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            NSLog(@"%@",dic);
        }else{
            NSLog(@"服务器内部出错");
        }
        
    }];
    
    
    
}


@end
